let htmlH = $('html').height() - 28,
//获取图表中的默认grid配置
defaultGrid = {top:'40px', left: '1%', right: '2%', bottom: '1%', containLabel: true},
defaultLabelOption = {
    show: true,
    position: 'insideBottom',
    distance: 15,
    align: 'left',
    verticalAlign: 'middle',
    rotate: 90,
    formatter: '{c}  {name|{a}}',
    fontSize: 24,
    rich: {
        name: {
            textBorderColor: '#fff',
            fontSize: 24
        }
    }
};
/**
 * 设置统计图高度，并返回高度
 */
function setChartHeight(id) {
    let chartH = htmlH > 720 ? 360 : (htmlH / 2);
    $('.chart div').height(chartH);
    $(id).css('margin-top',- chartH + 'px');
}
/**
 * 设置图表配置项
 * @param chartId 图表存放DIV的id
 */
function setChartOption(chartOption,chartId) {
    let e = echarts.init(I(chartId));
    e.setOption(chartOption,true);
}
/**
 * 画通用饼状图
 * @param pieTitle 饼状图标题
 * @param seriesName 鼠标移到饼状图上显示的标题
 * @param pieData 饼状图数据
 * @param pieId 饼状图存放的div id
 */
function drawCommonPie(pieTitle,seriesName,pieData,pieId) {
    let pieOption = {
        title: {
            text: pieTitle,
            x: 'center',
            left: 'left'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        grid: defaultGrid,
        series : [{
            name: seriesName,
            center: ['50%', '60%'],
            type: 'pie',    // 设置图表类型为饼图
            radius: '68%',  // 饼图的半径，外半径为可视区尺寸（容器高宽中较小一项）的 55% 长度。
            data: pieData
        }]
    };
    setChartOption(pieOption,pieId);
}
/**
 * 获取Rotation配置对象
 * @returns RotationOption配置对象
 */
function getRotationOptionObj(title,legendData,xAxisArr,seriesArr,yMax,yAxisShow,legendShow,yAxisName) {
    let titleObj = null;
    if (!isNull(title)){
        let titleLeft = isNull(yAxisName) ? 'left' : '30px';
        titleObj = {
            text: title,
            left: titleLeft
        };
    }
    return {
        title: titleObj,
        color: ['#003366', '#006699', '#4cabce', '#e5323e'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: defaultGrid,
        legend: {
            show: legendShow,
            data: legendData//城市数组
        },
        toolbox: {
            show: true,
            orient: 'vertical',
            left: 'right',
            top: 'center',
            feature: {
                mark: {show: true},
                dataView: {show: true, readOnly: false},
                magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        xAxis: [{
            type: 'category',
            axisTick: {show: false},
            data: xAxisArr//年份数组
        }],
        yAxis: [
            {
                type: 'value',
                show: yAxisShow,
                name: yAxisName,
                nameTextStyle:{
                    align: 'right'
                },
                max: function (v) {
                    if (!isNull(yMax)){
                        return yMax;
                    }
                    //返回Null,则表示自动计算Y坐标的刻度的最大值
                    return null;
                }
            }
        ],
        series: seriesArr//数据对象数组
    };
}

/**
 * 获取简单的柱状图配置对象
 * @param barTitle 柱状图标题
 * @param xAxisData 柱状图X轴数据，其值为一个数组
 * @param seriesData 柱状图Y轴数据，其值为一个数组
 * @returns 柱状图配置对象
 */
function getSimpleBarOption(barTitle,xAxisData,seriesData,yAxisName) {
    let titleLeft = isNull(yAxisName) ? 'left' : '30px';
    return {
        title: {
            text: barTitle,
            left: titleLeft
        },
        color: ['#e5323e'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'// 默认为直线，可选为：'line' | 'shadow'
            }
        },
        grid: defaultGrid,
        xAxis: [
            {
                type: 'category',
                data: xAxisData,
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis: [{
            type: 'value',
            name: yAxisName,
            nameTextStyle:{
                align: 'right'
            }
        }],
        series: [
            {
                type: 'bar',
                barWidth: '60%',
                data: seriesData
            }
        ]
    };
}

/**
 * 画多行拆线图
 * @param title 折线图标题
 * @param legendDataArr legend属性data值，折线图表上方的标识
 * @param xAxisDataArr xAxis属性data值，X轴坐标值
 * @param seriesDataArr series属性data值，Y轴坐标值
 * @param chartId 存放折线图的div id
 * @param legendLeft legend.left属性值，即legend距左边百分之多少，如30%，则写30
 * @param yAxisName Y轴单位
 */
function drawStackedLineChart(title,legendDataArr,xAxisDataArr,seriesDataArr,chartId,legendLeft,yAxisName) {
    let titleLeft = isNull(yAxisName) ? 'left' : '30px';
    let stackedLineOption = {
        title: {
            text: title,
            x: 'center',
            left: titleLeft
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: legendDataArr,
            x: 'center'
        },
        grid: defaultGrid,
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: xAxisDataArr
        },
        yAxis: {
            type: 'value',
            name: yAxisName,
            nameTextStyle:{
                align: 'right'
            }
        },
        series: seriesDataArr
    };
    if (!isNull(legendLeft)){
        stackedLineOption.legend.left = legendLeft + '%';
    }
    setChartOption(stackedLineOption,chartId);
}